<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $fields=array();
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_offlineapi_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goods_id']),array('price','title','image','total','status'));
			$item['price']=$goods['price'];
			$item['total']=$goods['total'];
			$item['goodstitle']=$goods['title'];
	}
	$htmls=array(
		'type'=>'list',	
		'tabs'=>array(
			array('op'=>'list','title'=>'套餐列表'),
			array('op'=>'edit','title'=>'添加套餐'),
		),		
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'卡套名称','type'=>'string'),
			array('name'=>'store_id','title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'goodstitle','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'price','title'=>'标价','type'=>'string'),
			array('name'=>'type','title'=>'标价','type'=>'radio','options'=>'types'),
			array('name'=>'createtime','title'=>'绑定时间','type'=>'time'),		
		)),
	);
	$return['htmls']=$htmls;
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['types']=$this->types;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['id']){
		$id=$_RQ['id'];
		$item=pdo_get('jy_offlineapi_goods',array('id'=>$id));
	}
	$return['item']=$item?:array('type'=>'0');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'套餐设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回套餐列表')
		),
		'edit'=>array(
				array('name'=>'title','title'=>'套餐名称','type'=>'string'),
				array('name'=>'api_gid','title'=>'套餐编号','type'=>'string','help'=>'对应的：API线下套餐编号'),
				array('name'=>'type','title'=>'套餐类型','type'=>'radio','options'=>'types'),
				array('name'=>'store_id','title'=>'所属门店','type'=>'select','options'=>'stores'),
				//array('name'=>'level_id','title'=>'绑定会员等级','type'=>'select','options'=>'levels','show'=>array('type'=>0)),
				array('name'=>'goods_id','title'=>'绑定商品','type'=>'choose','action'=>'jy_weishop/goods.get_goods/','help'=>'购买本商品后，店员核销时，会对应下线开通对应的权限（开卡/购买商品！）'),
				array('name'=>'status','title'=>'套餐状态','type'=>'switch','help'=>'这里的开关，只会影响接口开套餐，不影响商品购买！'),
		)
		);
		$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
		$return['types']=$this->types;
		$return['levels']=pdo_getall('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid']));
		$return['htmls']=$htmls;
		exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['goods_id']){
		exi('请绑定商品','error');
	}
	$data=array(
		'title'=>$postdata['title'],
		'goods_id'=>$postdata['goods_id'],
		'store_id'=>intval($postdata['store_id']),
		'api_gid'=>trim($postdata['api_gid']),
		'price'=>$postdata['price'],
		'type'=>$postdata['type'],
		'status'=>$postdata['status'],
	);
	if($postdata['id']){
		pdo_update('jy_offlineapi_goods',$data,array('id'=>$postdata['id']));
		exi('保存成功','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_offlineapi_goods',$data);
		$id=pdo_insertid();
		exi('添加成功','','list');
	}
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_offlineapi_goods',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_offlineapi_goods',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}	

?>